package org.erepublik.company.charts;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.erepublik.company.EAlliance;
import org.erepublik.company.EWorld;
import org.erepublik.company.Location;
import org.erepublik.company.comparators.HouseComparatorByWellness;
import org.erepublik.company.product.House;
import org.erepublik.company.searchCriteria.EHouseChartCriteria;
import org.erepublik.company.service.IVatService;
import org.erepublik.company.service.JSONHouseService;
import org.erepublik.company.service.XmlVatService;
import org.json.JSONArray;
import org.json.JSONException;


public class HouseJSONDataCollector extends AbstractJSONDataCollector<House> {

    private double SUM_ATTRIBUTE_LIMIT = 20000;

    public HouseJSONDataCollector(IVatService vatService) {
        super("house", EHouseChartCriteria.class, vatService);
    }

    @Override
    @SuppressWarnings("unchecked")
    protected double calculateOffers(House[] bestOffers, Enum criteria) {

        double attributeSum = 0.0d;
        double valueSum = 0.0d;
        for (House food : bestOffers) {


            valueSum += food.getPriceInGold() * food.getQuantity();
            attributeSum += food.getTotalRestoredWellness() * food.getQuantity();
            if (attributeSum>SUM_ATTRIBUTE_LIMIT )
                break;
        }
        if (valueSum==0)
            return 0.0d;
 
        
        return attributeSum / valueSum;
    }

    @Override
    @SuppressWarnings("unchecked")
    protected House[] getNBestOffers(int offers, ArrayList<House> object, Enum criteria, Location country) {

        House[] foodArray = filter(object, country ).toArray(new House[0]);
        
        Arrays.sort(foodArray, new HouseComparatorByWellness());
        House[] shorArray = new House[Math.min(offers, foodArray.length)];
        for (int i = 0; i < shorArray.length; i++) {
            shorArray[i] = foodArray[i];
        }
        return shorArray;
    }

    private ArrayList<House> filter(ArrayList<House> object, Location country) {
        
        if (country instanceof EAlliance) {
            ArrayList<House> newList = new ArrayList<House>();
            for (House house : object) {
                if (house.getCountry().getAlliance() == country)
                    newList.add(house);
            }
            return newList;
        }
        
        ArrayList<House> newList = new ArrayList<House>();
        for (House house : object) {
            if (country==EWorld.GLOBAL || house.getCountry() == country)
                newList.add(house);
        }

        return newList;
    }

    @Override
    protected ArrayList<House> getProductFromJSONArray(JSONArray jsonArrayWithFood) throws JSONException {
        return JSONHouseService.getHouseFromJSONArray(jsonArrayWithFood);
    }



    
    
    public static void main(String[] args) {
        HouseJSONDataCollector dataCollector = new HouseJSONDataCollector(new XmlVatService());
        dataCollector.updateData();
        ChartDataSet dataset = dataCollector.getData(EAlliance.EDEN, EHouseChartCriteria.WELLNESS.toString());
        List<ChartData> data = dataset.getData();
        for (ChartData chartData : data) {
            System.out.println( new Date(chartData.getTime()) + " "+chartData.getValue());
            
        }
    }
    
    
    
    
    
    


}
